Ext.namespace('iOS.ViewManager');

Ext.define('ViewManager', {
    
    /*
     * view列表，可以用addView将view注册到viewMap中，方便管理
     */
    viewMap  : {},
    
    getView : function(name){
        var view;
        if(typeof name == 'object'){
            view = name;
        } else {
            view = this.viewMap[name];
        }
        return view;
    },
    
    /*
     * 添加一个view到viewMap中
     */
    addView : function(name, view){
        if(arguments.length == 1){
            return;
        }
        this.viewMap[name] = view;
    },
    
    removeView : function(name){
        if(!name){
            return;
        }
        delete this.viewMap[name];
    },
    
    /*
     * 侦听事件，基于jQuery event 
     */
    listen: function(viewName, eventName, handler, scope){
        var view = this.getView(viewName);
        $(view).on('ios-' + eventName, function(e, param){
            scope[handler].apply(scope, param);
        });
    },
    
    /*
     * jQuery的trigger对如下例子也会生效
     * var obj = {
     *      test : function(){
     *           alert("1");
     *       }
     *  }
     *  $(obj).trigger("test"); //alert: 1;
     *  $(obj).on("test", function(){
     *       alert("2");
     *  }).trigger("test"); // alert: 2; alert: 1;
     *  只得在每个事件名称前加上“ios”前缀，防止此类事件发生 
     */
    notify: function(viewName, eventName, param){
        var view = this.getView(viewName);
        $(view).trigger('ios-' + eventName, [param]);
    }
});

iOS.ViewManager = new ViewManager();
